package com.xuexiang.xuidemo.provider;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSliderProvider;
import ohos.app.Context;

import com.isotne.glidelibrary.utils.OhosGlide;
import com.xuexiang.xuidemo.ResourceTable;

import java.util.ArrayList;
import java.util.List;

public class PageVideoSliderAdapter extends PageSliderProvider {
    private List<String> pages;
    private Context context;
    private AbilitySlice slice;
    OnItemClickListener mOnItemClickListener;

    public PageVideoSliderAdapter(ArrayList<String> pages, AbilitySlice slice, OnItemClickListener mOnItemClickListener, Context context) {
        this.pages = pages;
        this.context = context;
        this.slice = slice;
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public int getCount() {
        return pages == null ? 0 : pages.size();
    }


    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        ViewHolder viewHolder;
        //Component component = null;

        //if(component==null) {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_pager_video_item, null, false);
        if (!(component instanceof ComponentContainer)) {
            return null;
        }
        componentContainer.addComponent(component);
        viewHolder = new ViewHolder();
        viewHolder.image = (Image) componentContainer.findComponentById(ResourceTable.Id_image_pager);
        viewHolder.image_player = (Image) componentContainer.findComponentById(ResourceTable.Id_image_player);
        component.setTag(this);
        //}
       /* else{
            viewHolder = (ViewHolder)component.getTag();
        }*/
        //viewHolder.image.setPixelMap(pages.get(i));
        OhosGlide.with(slice).load(pages.get(i)).into(viewHolder.image);
        viewHolder.image.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mOnItemClickListener.onItemClick(component, i);
            }
        });
        viewHolder.image.setScaleMode(Image.ScaleMode.STRETCH);
//        ComponentTransition transition = new ComponentTransition();
        AnimatorProperty property = viewHolder.image.createAnimatorProperty();
        property.moveFromX(50).moveToX(1000).rotate(90).alpha(0).setDuration(2500).setDelay(500);


        return componentContainer;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        if (componentContainer == null) {
            return;
        }
        if (o instanceof Component) {
            componentContainer.removeComponent((Component) o);
        }
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return component == o;
    }

    public static class ViewHolder {
        public Image image;
        public Image image_player;
    }

    public interface OnItemClickListener {
        void onItemClick(Component component, int position);
    }
}
